package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.contract.cmd.ContractSignCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctAprvMsgCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctAprvSmsCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCertCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctFinQuotaCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctSerFeeDebitBatchSignCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctTranCmd;
import com.winchain.chainbill.bill.biz.discount.service.BillDctEventService;
import com.winchain.chainbill.bill.biz.receive.cmd.BillHldrCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.bill.biz.send.vo.BatchBillStats;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctAprvPassSubscriber extends OnErrorNotImplementedSubscriber<BatchBillEvent<String, CbDct>> {
	@Autowired
	private BillOptCmd<CbDct> billOptCmd;
	@Autowired
	private BillDctCmd billDctCmd;
	@Autowired
	private BillMoveCmd<CbDct> billMoveCmd;
	@Autowired
	private BillDctTranCmd billDctTranCmd;
	@Autowired
	private BillDctEventService billDctEventService;
	@Autowired
	private ContractSignCmd<CbDct> contractSignCmd;
	@Autowired
	private BillDctAprvMsgCmd billDctAprvMsgCmd;
	@Autowired
	private BillDctAprvSmsCmd billDctAprvSmsCmd;
	@Autowired
	private BillHldrCmd billHldrCmd;
	@Autowired
	private BillDctCertCmd billDctCertCmd;
	@Autowired
	private BillDctFinQuotaCmd billDctFinQuotaCmd;
	@Autowired
	private BillDctSaveSerFeeDebitDetailListCmd billDctSaveSerFeeDebitDetailListCmd;
	@Autowired
	private BillDctSerFeeDebitBatchSignCmd billDctSerFeeDebitBatchSignCmd;

	public void onApplicationEvent(BatchBillEvent<String, CbDct> t) {
		List<String> records = t.getRecs();
		BatchBillStats<CbDct> stats = t.getStats();

		Bill<CbDct> source = new Bill();
		List<AcctTxRec> acctTxRecs = new ArrayList();
		for (String record : records) {
			try {
				BillTranEvent<CbDct> event = this.billDctEventService.getBillTranEvent(record, false);
				event.setOperation(t.getOperation());
				source.setRec(event.getBill().getRec());

				Bill<CbDct> prntBill = event.getBill();

				List<Bill<CbDct>> children = (List<Bill<CbDct>>) event.getChildren();

				CommandContext<Bill<CbDct>> context = new CommandContext();
				context.setSource(prntBill);
				context.setValue("Operation", t.getOperation());
				context.setValue("TimeoutOperation", t.getTimeoutOpt());

				context.setValue("bizType", "cb_dct_apr");
				context.setValue("children", children);
				context.setValue("phoneCode", t.getValue("phoneCode"));
				context.setValue("valRetBizState", "0");
				context.setValue("OwnerId", ((CbDct) prntBill.getSource()).getFinId());
				context.setValue("OwnerName", ((CbDct) prntBill.getSource()).getFinNm());
				context.setValue("quotaOptType", "qt_use");
				((CbDct) prntBill.getSource()).setDctQtOpt("2");

				ChainBase<Bill<CbDct>> crb = new ChainBase();
				if (!ChainBillUtil.signContOnline()) {
					crb.add(this.contractSignCmd);
					context.setValue("zySign", "1");
					crb.add(this.billDctSerFeeDebitBatchSignCmd);
				}
				crb.add(this.billDctSaveSerFeeDebitDetailListCmd);
				crb.add(this.billOptCmd);
				crb.add(this.billHldrCmd);
				crb.add(this.billMoveCmd);

				crb.add(this.billDctFinQuotaCmd);

				crb.add(this.billDctCmd);
				crb.add(this.billDctTranCmd);

				crb.add(this.billDctCertCmd);
				crb.add(this.billDctAprvMsgCmd);
				crb.add(this.billDctAprvSmsCmd);
				crb.execute(context);
				acctTxRecs.add((AcctTxRec) context.getValue("acctTxRec"));
				Object serFeeTxRec = context.getValue("serFeeTxRec");
				if (serFeeTxRec != null) {
					acctTxRecs.add((AcctTxRec) serFeeTxRec);
				}
				t.setValue("acctTxRecs", acctTxRecs);
				stats.setAcctTxRecs(acctTxRecs);
				stats.addPassedRecord(source);
			} catch (Exception e) {
				e.printStackTrace();
				stats.addAbnormalRecord(source);
				stats.addAbnormalMsg(e.getMessage());
			}
		}
	}
}
